package com.eryos.android.holidaybudget;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.eryos.android.holidaybudget.beans.Budget;
import com.eryos.android.holidaybudget.beans.Summary;

/**
 * Project :  Holiday'sBudget
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/Holiday'sBudget/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class SummaryActivity extends RootActivity {

	ArrayList<Summary> listOfSummary;
    TextView budgetNameField;
    TextView initialAmountField;
    TextView totalExepensesField;
    TextView remainingField;
    Button detailsButton;
    
	@Override
    protected void initParam(){
		Log.d(LOG_TAG, "initParam");

		viewTitle = "Summary";
		hasTopBanner = true;
	}

    @Override
    protected void initUI(){
        Log.d(LOG_TAG, "initUI");
 
        /* Define the Layout */
        setContentView(R.layout.summary);
        
        showWaitingMessage();
    	
        /* Retrieve UI elements */
        budgetNameField 	= (TextView) findViewById(R.id.summary_budget_value);
        initialAmountField	= (TextView) findViewById(R.id.summary_initial_amount_value);
        totalExepensesField = (TextView) findViewById(R.id.summary_total_expenses_value);
        remainingField 		= (TextView) findViewById(R.id.summary_remaining_value);
        detailsButton 		= (Button) findViewById(R.id.summary_details_button);

    }
    
    @Override
    protected void initHandlers(){
    	super.initHandlers();
    	
    	Log.d(LOG_TAG, "initHandlers");

    	detailsButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Log.d(LOG_TAG, "Click Detail Button");
            }
        });
    
    }

    @Override
    protected void fetchData(){
        Log.d(LOG_TAG, "fetchData");

        // Retrieve data in background thread
        new BackgroundTask().execute("AAAA");
    }

    @Override
	protected void refreshUI() {
    	Log.d(LOG_TAG, "refreshUI");

    	for (Summary sum : listOfSummary)
    		addBudgetSummary(sum.getBudget().getName(), sum.getBudget().getInitialAmount(), sum.getTotalExpenses());
    	
    	hideWaitingMessage();
	}
    
    private void addBudgetSummary(String budgetName, double budgetAmount, double expenseAmount){
        budgetNameField.setText(budgetName);
        initialAmountField.setText(Double.toString(budgetAmount));
        totalExepensesField.setText(Double.toString(expenseAmount));
        double remain = budgetAmount - expenseAmount;
        remainingField.setText(Double.toString(remain));
    }

    /**
     * 
     * String : arguments type
     * Integer : progress value
     * Bitmap : return type
     */
	private class BackgroundTask extends AsyncTask<String, Integer, ArrayList<Summary>> { 

		/** 
		 * Step 1 - invoked on the UI thread immediately after the task is executed. 
		 * This step is normally used to setup the task, for instance by showing a progress bar in the user interface 
		 */
		protected void onPreExecute() { 
	        Log.d(LOG_TAG, "BG 1/3 - onPreExecute");
		} 

		/** 
		 * Step 2 - invoked on the background thread immediately after onPreExecute() finishes executing. 
		 * This step is used to perform background computation that can take a long time 
		 */
		protected ArrayList<Summary> doInBackground(String... budgetNames) { 
	        Log.d(LOG_TAG, "BG 2/3 - doInBackground");

	        listOfSummary = new ArrayList<Summary>();
	        for ( String budgetName : budgetNames ){
	        	Summary sum = new Summary();
	        	sum.setTotalExpenses(db.getTotalExpenses(budgetName));
	        	sum.setBudget(db.getBudget(budgetName));
	        	listOfSummary.add(sum);
	        }

			return listOfSummary;
		} 

		/** 
		 * Step 3 - invoked on the UI thread after the background computation finishes. 
		 * The result of the background computation is passed to this step as a parameter. 
		 */
		protected void onPostExecute(ArrayList<Summary>  result) {
	        Log.d(LOG_TAG, "BG 3/3 - onPostExecute");

	        refreshUI();
		}
		
		 protected void onProgressUpdate(Integer... progress) {
		        Log.d(LOG_TAG, "BG - onProgressUpdate");
	         //setProgressPercent(progress[0]);
	     }
	}

}
